package com.cn.wanxi.dao.impl;

import com.cn.wanxi.dao.UserDao;
import com.cn.wanxi.model.UserModel;
import com.cn.wanxi.util.GetSqlSession;
import com.cn.wanxi.util.JDBC;
import com.cn.wanxi.util.MyTool;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class UserImpl implements UserDao {
    /**
     * 1.封装sql语句
     * 2.执行sql语句  调用JDBC
     * 3.封装返回结果
     *
     * @param userModel
     * @return
     */
    @Override
    public int add(UserModel userModel) {

        return GetSqlSession.getSqlSession().insert("com.cn.wanxi.dao.UserDao.add", userModel);
    }

    @Override
    public UserModel findByUsername(String aa) {
        return GetSqlSession.getSqlSession().selectOne("com.cn.wanxi.dao.UserDao.findByUsername", aa);
    }

    @Override
    public List<UserModel> findAll() {
//        return GetSqlSession.getSqlSession().selectList("com.cn.wanxi.dao.UserDao.findAll");
        return null;
    }

    @Override
    public List<UserModel> findAll(UserModel userModel) {

        return GetSqlSession.getSqlSession().selectList("com.cn.wanxi.dao.UserDao.findAll", userModel);
    }

    /**
     * 查询多少条数据
     *
     * @return
     */
    public int getCount(UserModel userModel) {

        return GetSqlSession.getSqlSession().selectOne("com.cn.wanxi.dao.UserDao.getCount", userModel);
    }

    private List<UserModel> setList(String sql) {
        ResultSet resultSet = JDBC.query(sql);
        List<UserModel> list = new ArrayList<>();
        try {
            while (resultSet.next()) {
                UserModel userModel = new UserModel();
                userModel.setId(resultSet.getInt("id"));
                userModel.setUsername(resultSet.getString("username"));
                userModel.setPhone(resultSet.getString("phone"));
                userModel.setEmail(resultSet.getString("email"));
                userModel.setSex(resultSet.getString("sex"));
                userModel.setBirthday(resultSet.getString("birthday"));
                userModel.setHobby(resultSet.getString("hobby"));
                userModel.setHeight(resultSet.getInt("height"));
                userModel.setCreateTime(resultSet.getString("create_time"));
                userModel.setEnable(resultSet.getInt("enable"));
                list.add(userModel);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public UserModel findByUsernameAndPassword(UserModel userModel) {

        return GetSqlSession.getSqlSession().selectOne("com.cn.wanxi.dao.UserDao.findByUsernameAndPassword", userModel);
    }

    @Override
    public int del(Integer id) {

        return GetSqlSession.getSqlSession().delete("com.cn.wanxi.dao.UserDao.del", id);
    }

    @Override
    public UserModel findById(int id) {

        return GetSqlSession.getSqlSession().selectOne("com.cn.wanxi.dao.UserDao.findById", id);
    }


}
